package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.common.utils.StringUtils;
import com.learn.core.domain.*;
import com.learn.core.mapper.*;
import com.learn.core.service.ILLevelPaperService;
import com.learn.framework.service.impl.BaseServiceImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LLevelPaperServiceImpl
 * @date 2021.07.10 14:18
 */
@Service
public class LLevelPaperServiceImpl extends BaseServiceImpl<LLevelPaperMapper, LLevelPaper> implements ILLevelPaperService {
    @Autowired
    LQuestionsMapper lQuestionsMapper;
    @Autowired
    LLevelPaperMapper lLevelPaperMapper;
    @Autowired
    LLevelSonMapper lLevelSonMapper;
    @Autowired
    LLevelQuestionsMapper lLevelQuestionsMapper;

    @Override
    public void levelPaper(Integer id, String userId) {

        QueryWrapper<LLevelSon> levelQuery = new QueryWrapper<LLevelSon>();
        levelQuery.eq("level_id",id);
        List<LLevelSon> list = lLevelSonMapper.selectList(levelQuery);

        for (LLevelSon lLevelSon: list) {
            // 删除原生成试卷
            QueryWrapper<LLevelPaper> paperQuery = new QueryWrapper();
            paperQuery.eq("level_son_id", lLevelSon.getLevelSonId());
            lLevelPaperMapper.delete(paperQuery);

            //TODO 根据考次规定生成试卷
            QueryWrapper<LLevelQuestions> questionsQuery = new QueryWrapper<LLevelQuestions>();
            questionsQuery.eq("level_son_id", lLevelSon.getLevelSonId());
            List<LLevelQuestions> questions = lLevelQuestionsMapper.selectList(questionsQuery);
            Map<String, Object> map = new HashMap<>();
            for (LLevelQuestions lLevelQuestions : questions) {
                if (1 == lLevelQuestions.getQuestionsTypeId()) {
                    map.put("radio", levelQuestionsList(lLevelQuestions,lLevelQuestions.getNumber(),lLevelQuestions.getQuestionsTypeId()));
                } else if (2 == lLevelQuestions.getQuestionsTypeId()) {
                    map.put("check", levelQuestionsList(lLevelQuestions,lLevelQuestions.getNumber(),lLevelQuestions.getQuestionsTypeId()));
                } else if (3 == lLevelQuestions.getQuestionsTypeId()) {
                    map.put("judge", levelQuestionsList(lLevelQuestions,lLevelQuestions.getNumber(),lLevelQuestions.getQuestionsTypeId()));
                }
            }
            JSONObject jsonObject = JSONObject.fromObject(map);

            // 生产试卷
            LLevelPaper levelPaper = new LLevelPaper();
            levelPaper.setLevelSonId(lLevelSon.getLevelSonId());
            levelPaper.setLevelQuestionsId(jsonObject.toString());
            levelPaper.setCreateTime(new Date());
            levelPaper.setCreateUser(userId);
            lLevelPaperMapper.insert(levelPaper);
        }
    }

    private List<Integer> levelQuestionsList(LLevelQuestions lLevelQuestions, Integer number, Integer questionsTypeId){
        List<Integer> stringList = new ArrayList<>();
        QueryWrapper<LQuestions> lQuestionsQueryWrapper = new QueryWrapper<LQuestions>();
        lQuestionsQueryWrapper.eq("questions_type",questionsTypeId);
        if(StringUtils.isNotEmpty(lLevelQuestions.getKnowledge()))
            lQuestionsQueryWrapper.like("knowledge",lLevelQuestions.getKnowledge());
        if(StringUtils.isNotEmpty(lLevelQuestions.getDifficulty()))
            lQuestionsQueryWrapper.eq("questions_difficulty",lLevelQuestions.getDifficulty());
        lQuestionsQueryWrapper.orderByAsc("rand()");
        lQuestionsQueryWrapper.last(" limit " + number);
        List<LQuestions> lQuestionsList = lQuestionsMapper.selectList(lQuestionsQueryWrapper);
        for (LQuestions lQuestions : lQuestionsList) {
            stringList.add(lQuestions.getQuestionsId());
        }
        return stringList;
    }
}
